package com.util.utillib.extend

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity

enum class IntentFlagMode(val type: Int) {
    SINGLE_TOP(type = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK),
    NEW_TASK(type = Intent.FLAG_ACTIVITY_NEW_TASK)
}

/**
 */
fun AppCompatActivity.toNextActivity(
    activity: Class<out AppCompatActivity>? = null,
    mode: IntentFlagMode = IntentFlagMode.NEW_TASK,
    needFinish: Boolean = false,
    params: HashMap<String, String> = HashMap(),
    resultCode: Int = 509
) {
    val next = activity ?: return
    try {
        startActivityForResult(Intent(this, next).apply {
            addFlags(mode.type)
            params.keys.forEach { putExtra(it, params[it]) }
        }, resultCode)

        if (needFinish and (mode == IntentFlagMode.NEW_TASK)) {
            finish()
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
